<template>
  <div class="top-right-btn" :style="style">
    <el-row>
      <el-tooltip
        class="item"
        effect="dark"
        :content="showSearch ? '隐藏搜索' : '显示搜索'"
        placement="top"
        v-if="search">
        <el-button circle icon="Search" @click="toggleSearch()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <el-button circle icon="Refresh" @click="emit('refresh')" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="显隐列" placement="top" v-if="columns">
        <el-dropdown trigger="click" :hide-on-click="false" style="padding-left: 12px">
          <el-button circle icon="Menu" />
          <template #dropdown>
            <el-dropdown-menu>
              <template v-for="item in columns">
                <el-dropdown-item>
                  <el-checkbox :checked="item.show" @change="checkboxChange($event, item.key)" :label="item.label" />
                </el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-tooltip>
    </el-row>
  </div>
</template>

<script setup name="RightToolbar">
  const props = defineProps({
    /* 是否显示检索图标 */
    search: {
      type: Boolean,
      default: true
    },
    /* 右外边距 */
    gutter: {
      type: Number,
      default: 10
    }
  })
  const columns = defineModel('columns')
  const emit = defineEmits(['refresh'])

  // 勾选
  function checkboxChange(event, key) {
    columns.value.filter(item => item.key === key)[0].show = event
  }

  /* 是否显示检索条件 */
  const showSearch = defineModel('showSearch', { default: true })
  function toggleSearch() {
    showSearch.value = !showSearch.value
  }

  // 弹出层标题
  const style = computed(() => {
    const ret = {}
    if (props.gutter) {
      ret.marginRight = `${props.gutter / 2}px`
    }
    return ret
  })
</script>

<style lang="scss" scoped>
  :deep(.el-transfer__button) {
    border-radius: 50%;
    display: block;
    margin-left: 0px;
  }
  :deep(.el-transfer__button:first-child) {
    margin-bottom: 10px;
  }
  :deep(.el-dropdown-menu__item) {
    line-height: 30px;
    padding: 0 17px;
  }
</style>
